import json

def login(name, passwd):
    info = {"passwd": passwd}
    info.update({"gb": 0})
    new_user = {name: info}
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r+", encoding="UTF-8") as file:
            file_1 = file.read()
            if file_1 != "":
                user_info = json.loads(file_1)
                new_user.update(user_info)
            else:
                new_user = new_user
            file.seek(0)
            file.write(json.dumps(new_user))
            return True
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def login1(name, passwd):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r") as file:
            file_1 = file.read()
            if file_1 == "":
                return False
            user_info = json.loads(file_1)
            if name not in user_info:
                return False
            if passwd == user_info[name]["passwd"]:
                return True
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")


def verify(name):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r") as file:
            file_1 = file.read()
            if file_1 == "":
                return False
            user_info = json.loads(file_1)
            if name in user_info:
                return True
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def transfer(name, name2, num):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r+", encoding="UTF-8") as file:
            int(num)
            user_info = json.loads(file.read())
            if name2 not in user_info:
                return print("用户不存在")
            user_info[name]["gb"] = user_info[name]["gb"] - int(num)
            user_info[name2]["gb"] = user_info[name2]["gb"] + int(num)
            if user_info[name]["gb"] < 0:
                return print("余额不足")
            file.seek(0)
            file.write(json.dumps(user_info))
            return True
    except ValueError:
        print("请输入正常金额")
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def deposit(name, num):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r+", encoding="UTF-8") as file:
            int(num)
            user_info = json.loads(file.read())
            user_info[name]["gb"] = user_info[name]["gb"] + num
            file.seek(0)
            file.write(json.dumps(user_info))
            return True
    except ValueError:
        print("请输入正常金额")
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def withdraw(name, num):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r+", encoding="UTF-8") as file:
            user_info = json.loads(file.read())
            int(num)
            user_info[name]["gb"] = user_info[name]["gb"] - num
            if user_info[name]["gb"] < 0:
                return print("余额不足")
            file.seek(0)
            file.write(json.dumps(user_info))
            return True
    except ValueError:
        print("请输入正常金额")
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def gb(name):
    file_name = "atm_info.txt"
    try:
        with open(file_name, "r") as file:
            user_info = json.loads(file.read())
            return user_info[name]["gb"]
    except json.decoder.JSONDecodeError:
        print("文件解析错误，请检查文件内容是否正确")

def atm_1(name):
    while True:
        choice = input(f"你的余额为：{gb(name)}\n1,存钱\n2,取钱\n3,转账\n4,退出\n请选择你的操作：")
        if choice == "1":
            num = input("请输入你要存的金额：")
            if deposit(name, int(num)):
                print("成功存入")
        elif choice == "2":
            num = input("请输入你要取的金额：")
            if withdraw(name, int(num)):
                print("成功取出")
        elif choice == "3":
            name2 = input("请输入你要转账的用户：")
            num = input("请输入你要转账的金额：")
            if transfer(name, name2, num):
                print("转账成功")
        elif choice == "4":
            break
        else:
            print("请输入正确选项")